using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PadnugDomain = padnug.Domain;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using padnug.Domain;
using PadnugModels = padnug.Models.Home;
using PadnugService;

namespace padnug.Controllers
{
    public class EventsController : Controller
    {
        private IPadnugCalendarService _calendarService;
        private IPadnugEventsService _eventService;
        private IPadnugSponsorService _sponsorService;
        public EventsController() : this(new PadnugCalendarService(), new PadnugEventsService(), new PadnugSponsorService())
        {

        }

        public EventsController(IPadnugCalendarService calendarService, IPadnugEventsService padnugEventsService, IPadnugSponsorService padnugSponsorService)
        {
            _calendarService = calendarService;
            _eventService = padnugEventsService;
            _sponsorService = padnugSponsorService;
        }
        //
        // GET: /Event/

        public ActionResult Index(int? skip,int? take)
        {
            skip = skip.HasValue ? skip : 0;
            take = take.HasValue ? take : 15;

            var featuredPresentation = GetFeaturedEvent();
            var upcomingPresentations = GetUpcomingEvents();
            var featuredSponsors = GetPremiumSponsors();
            var eventNotes = GetEventNotes();

            var model = new PadnugModels.IndexViewModel
            {
                FeaturedPresentation = featuredPresentation,
                UpcomingPresentations = upcomingPresentations,
                FeaturedSponsors = featuredSponsors,
                EventNotes = eventNotes
            };

            ViewData["FeaturedSponsors"] = model.FeaturedSponsors;
            var eventDates = _calendarService.GetAllEventDates();

            ViewData["EventDates"] = eventDates;
            ViewData.Model = _eventService.GetEvents(skip, take);
            return View();
        }

        public ActionResult Event(int Id)
        {
            var padnugEvent = _eventService.GetEvent(Id);

            ViewData.Model = padnugEvent;

            return View("Event");

        }

        public ActionResult EventsOnDate(DateTime date)
        {
            var padnugEvents = _eventService.GetEventsForADate(date);
            
            var featuredPresentation = GetFeaturedEvent();
            var upcomingPresentations = GetUpcomingEvents();
            var featuredSponsors = GetPremiumSponsors();
            var eventNotes = GetEventNotes();
            var eventDates = _calendarService.GetAllEventDates();

            ViewData["EventDates"] = eventDates;
            ViewData.Model = padnugEvents;
            ViewData["FeaturedSponsors"] = featuredSponsors;
            ViewData["eventNotes"] = eventNotes;
            return View("Events");

        }

        public ActionResult GetCalendarEvents()
        {

            IList<PadnugDomain.CalendarEvent> events = new List<PadnugDomain.CalendarEvent>();
            ActionResult result;
            if (System.Web.HttpContext.Current.Session["CalendarEvents"] == null)
            {
                result = new HomeController().GetCalendarEvents();
            }
            else
            {
                events = (List<PadnugDomain.CalendarEvent>)System.Web.HttpContext.Current.Session["CalendarEvents"];
                result = Json(events);
            }

            return result;
        }

        private IEnumerable<PadnugModels.Sponsor> GetPremiumSponsors()
        {
            var list = new List<PadnugModels.Sponsor>();

            IList<PadnugDomain.Sponsor> padnugPremiumSponsors = _sponsorService.GetPremimumSponsors();

            foreach (var p in padnugPremiumSponsors)
            {
                PadnugModels.Sponsor s = new PadnugModels.Sponsor { LogoUrl = p.SponsorLargeImage, Name = p.SponsorName, Website = p.SponsorURL };
                list.Add(s);
            }

            return list;
        }

        private PadnugModels.Sponsor GetSponsor()
        {
            var sponsor = new PadnugModels.Sponsor
            {
                LogoUrl = "/content/images/microsoft.png",
                Name = "Microsoft, Inc",
                Website = "http://www.microsoft.com"
            };

            return sponsor;
        }

        private IEnumerable<PadnugModels.Presentation> GetUpcomingEvents()
        {
            var list = new List<PadnugModels.Presentation>();

            IList<Event> eventsList = _eventService.GetUpcomingEvents();
            foreach (var e in eventsList)
            {
                var p = new PadnugModels.Presentation
                {
                    EventDate = e.MeetingStart.HasValue ? e.MeetingStart.Value : DateTime.Now,
                    OrganizationName = e.OrganizationName,
                    SpeakerName = e.SpeakerName,
                    SpeakerPhotoUrl = e.SpeakerPhotoUrl,
                    Title = e.Topic
                };

                list.Add(p);
            }

            return list;
        }

        private PadnugModels.Presentation GetFeaturedEvent()
        {
            Event e = _eventService.GetFeaturedEvent();
            var featuredPresentation = new PadnugModels.Presentation
            {
                EventDate = e.MeetingStart.HasValue ? e.MeetingStart.Value : DateTime.Now,//DateTime.Now.AddDays(5),
                OrganizationName = e.OrganizationName,
                SpeakerName = e.SpeakerName,
                SpeakerPhotoUrl = e.SpeakerPhotoUrl,
                Title = e.Topic//,
             //   EventDetailsHtml = @"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                                    //<p>
                                    //    <strong>Time:</strong><br />
                                    //    6:00pm Pizza (by Ascentium)<br />
                                    //    6:30pm Presentation<br />
                                    //    9:00pm Afterwards at Gustav's
                                    //</p>
                                    //<p>
                                    //    <strong>Location:</strong><br />
                                    //    Microsoft Portland Office located in Lincoln Tower<br />
                                    //    10260 SW Greenburg Road<br />
                                    //    Suite 600<br />
                                    //    Portland, OR 97223
                                    //</p>"
            };

            return featuredPresentation;
        }

        private IEnumerable<PadnugModels.EventNote> GetEventNotes()
        {
            var list = new List<PadnugModels.EventNote>();

            for (int i = 0; i < 3; i++)
            {
                var eventNote = new PadnugModels.EventNote
                {
                    Title = "OTN: Certified Scrum Master Training",
                    EventDate = "April 22 and April 23, 9:00am-5:00pm",
                    Url = "http://www.sao.org/events/event_details.asp?id=102320"
                };

                list.Add(eventNote);
            }

            return list;
        }
    }
}
